<?php
declare(strict_types=1);
namespace Service\ConsumerService;
use Service\BaseService;
use App\Model\PssLogModel\MessageUserSkuChangeLog;
use Service\Code\Normal;

/**
 * 推送下游渠道消息消费功能问题
 * @author chenshubo
 */
class SkuChangeLogMessageService extends BaseService
{
    /**
     * 处理sku消息变化日志
     * @param array $param
     */
    public function syncChangeAddLog(array $param = []) 
    {
        $bid            = (int)$param['bid'];
        $message_id     = (int)$param['message_id'];
        $message_type   = $param['message_type'];
        $push_response  = $param['push_response'];
        $contents       = $param['contents'];
        return $this->handleUserSkuChangeLog($bid,$message_id,$contents,$message_type,$push_response);
    }
    
    /**
     * 写入每个商品SKU变化日志
     * 商品更新、下架、批量下架操作、品牌、分类、国家、单位、标签
     * @param int $bid
     * @param int $message_id
     * @param string $contents
     * @param string $message_type
     * @param array $push_response
     */
    public function handleUserSkuChangeLog( int $bid,int $message_id,string $contents,string $message_type,array $push_response)
    {
        $contentsArr    = @json_decode($contents, true);
        $data           = $contentsArr['data'];
        $soure          = $contentsArr['Source'] ?? 1;
        $created_at     = date('Y-m-d H:i:s');
        $saveData       = [];
        if( $message_type == 'goodsSkuChange' )//商品下架
        {
            //商品下架
            $skuList = $data['skuList'];
            foreach ($skuList as $value)
            {
                $tempGoods = $data;
                $tempGoods['skuList'] = [$value];
                $saveData[] = [
                    'source'        => $soure,
                    'bid'           => $bid,
                    'skucode'       => $value,
                    'contents'      => json_encode($tempGoods,JSON_UNESCAPED_UNICODE),
                    'used_time'     => $push_response['used_time'],
                    'push_status'   => $push_response['push_status'],
                    'push_result'   => $push_response['push_result'],
                    'push_url'      => $push_response['push_url'],
                    'message_type'  => $message_type,
                    'created_at'    => $created_at
                ];
            }
        } else if( $message_type == 'goodsChange' ) {//商品更改
            foreach ($data as $goodsItem)
            {
                foreach ($goodsItem['skuList'] as $skuItem)
                {
                    $tempGoods = $goodsItem;
                    $tempGoods['skuList'] = [$skuItem];
                    $saveData[] = [
                        'source'        => $soure,
                        'bid'           => $bid,
                        'skucode'       => $skuItem['skucode'],
                        'contents'      => json_encode($tempGoods,JSON_UNESCAPED_UNICODE),
                        'used_time'     => $push_response['used_time'],
                        'push_status'   => $push_response['push_status'],
                        'push_result'   => $push_response['push_result'],
                        'push_url'      => $push_response['push_url'],
                        'message_type'  => $message_type,
                        'created_at'    => $created_at,
                    ];
                }
            }
        } else {//其它类型
            $saveData[] = [
                'bid'           => $bid,
                'skucode'       => '0',
                'contents'      => $contents,
                'used_time'     => $push_response['used_time'],
                'push_status'   => $push_response['push_status'],
                'push_result'   => $push_response['push_result'],
                'push_url'      => $push_response['push_url'],
                'message_type'  => $message_type,
                'created_at'    => $created_at,
            ];
        }
        
        if($saveData)
        {
            MessageUserSkuChangeLog::instance()->insert($saveData);
        }
        
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
}